<?php
namespace app\admin\controller;

use think\Controller;
use Common\Controller\BaseController;
use think\Request;
use think\Session;

/*
 * 2019-03-14-11:59
 * name:DMH
 *
 * */
class Login extends Controller
{
//跳转登陆页面开始
    public function login()
    {
        $user = session('user');
        //dump($user);exit;

        if ($user) {
            header("Refresh:1;url=../index/index");
        }

        return $this->fetch();
    }
//跳转登陆页面结束
//验证登陆开始开始
    public function loginfind()
    {
        $user = session('user');
        //dump($user);exit;
        //return show(1, '登录失败');

        if ($user) {
            header("Refresh:1;url=../index/index");
        }

        if (!$_POST) {
            $this->success('请输入正确的账号账号密码', 'login/login');
            //echo "<script>alert('');javascript:history.go(-1);</script>";
            // $this->success('请输入正确的账号账号密码','login');
        }
        $data['name'] = trim($_POST['username']);
        $data['password'] = md5(md5(trim($_POST['password'])));

        $res = db('admin')->where($data)->find();
        if ($res['password'] == $data['password']) {
            $data = array(
                'admin_id' => $res['id'],
                'name' => $res['name'],
                'jurisdiction' => $res['jurisdiction']
            );
            session('user', $data);  //设置session
            $this->success('登陆成功', 'index/index');

        } else {
            $this->success('登陆失败', 'login/login');

        }
    }
//验证登陆开始结束
//注销登录开始
    public function loginout()
    {
        session('user', null);
        echo "<script>alert('退出登陆成功');location.href='login';</script>";
    }
//注销登录结束
}